import express from "express"
import mongoose from "mongoose"
import dotenv from "dotenv"
import userRoutes from "./routes/users.js"
import commentRoutes from "./routes/comments.js"
import videoRoutes from "./routes/videos.js"
import authRoutes from "./routes/auth.js"
import cookieParser from "cookie-parser";

const app=express()
dotenv.config()

const connect=()=>{
    mongoose.connect(process.env.MONGO).then(res=>{
        console.log("连接到了mongodb");
    }).catch(err=>{
        throw err;
    })
}
app.use(cookieParser())
app.use(express.json())
app.use("/api/users",userRoutes)
app.use("/api/comments",commentRoutes)
app.use("/api/videos",videoRoutes)
app.use("/api/auth",authRoutes)

app.use((err,req,res,next)=>{
  const status=err.status || 500;
  const message=err.message || "发生错误!";
  return res.status(status).json({
    success:false,
    status,
    message
  })
})

app.listen(8800,()=>{
    connect();
    console.log("服务器已经启动：端口"+8800)
})